<?php
/**
 * 包装箱（地区）
 */

namespace App\Admin\Controllers\Basics;

use App\Models\Basics\PackingBox;
use App\Models\Basics\SysIArea;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Database\Eloquent\Collection;
class PackingBoxController extends AdminController{

    protected $title = '产品包装箱设置';

    protected function grid()
    {
        $grid = new Grid(new PackingBox());
        $grid->model()->orderBy('id','DESC');
        $grid->model()->collection(function (Collection $collection){
            foreach($collection as $item) {
                $item->area = $item->packing_box_area->pluck('area');
            }
            return $collection;
        });
        $grid->column('box_type','箱子类型')->help('可模糊查询');
        $grid->column('area','关联站点')->label();

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->quickSearch('box_type');
        $grid->disableFilter();

        return $grid;
    }

    public function form()
    {
        $form = new Form(new PackingBox());

        $form->hidden('id');

        $form->text('box_type', '箱子类型')
            ->creationRules(['required', "unique:b_packing_box"],['required' => '必填','unique' => '已存在'])
            ->updateRules(['required', "unique:b_packing_box,box_type,{{id}}"],['required' => '必填','unique' => '已存在']);

        $form->multipleSelect('packing_box_area', '站点')
            ->options(SysIArea::options())
            ->required()
            ->rules("required");

        $form->hidden('created_at');
        $form->hidden('updated_at');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
